package logic

import (
	"backend/apps/book/models"
	"context"
	"time"

	"backend/apps/book/proto"
	"backend/apps/book/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateChapterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateChapterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateChapterLogic {
	return &CreateChapterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreateChapter 章节相关操作
func (l *CreateChapterLogic) CreateChapter(in *proto.Chapter) (*proto.CommonResponse, error) {
	now := time.Now().Unix()
	_, err := l.svcCtx.ChapterModel.Insert(l.ctx, &models.Chapter{
		BookId:        in.BookId,
		Title:         in.Title,
		ChapterNumber: int64(in.ChapterNumber),
		PublishTime:   time.Time{},
		IsFree:        int64(in.IsFree),
		CreateTime:    now,
		UpdateTime:    now,
	})
	return &proto.CommonResponse{}, err
}
